Clearing Floats with Pseudo-Elements in CSS
Pseudo-elements like ::before and ::after can be used to clear floated child elements without adding extra HTML markup. This is a common technique called the 'clearfix' method.
Floated elements are removed from the normal document flow, which can cause their parent container to collapse.
Using ::after with content: '' and display: table or block allows the parent to wrap around floated children.
Applying clear: both to the pseudo-element ensures that it clears all preceding floats.
This method avoids adding extra empty elements in HTML solely for clearing purposes.
In this example, the .container uses ::after to clear the floated .item elements. This ensures the container wraps around its floated children without needing extra HTML elements.
Use ::after with content: '', display: table, and clear: both for the classic clearfix.
Keep HTML semantic and avoid adding empty <div>s just for clearing floats.
Test across different browsers, especially older ones, to ensure the clearfix works consistently.
Consider modern layout methods like Flexbox or Grid which often remove the need for floats and clearfixes.